<?php
use Workerman\Mysql;

class Token{
    
    
    /**
     * 生成用户 Token
     *
     * @param  $user_id
     * @return $access_token
     */
    
    
    private static function createToken($user_id)
    {
          // hmacsha256签名
          $obj = new \stdClass();
          $obj-> user_id = $user_id;
          $user_str = json_encode($obj);
          $hmac_sha256 = hash_hmac("sha256", $user_str."sctmesSCTMES".time(),"secret");
          $access_token = base64_encode($hmac_sha256);
          return $access_token;
    }

    /**
     * 登录账号
     *
     * @param  $user,$password
     * @return array
     */
    public static function login($user,$password)
    {
         // 判断账号、密码是否正确
         $sql = "SELECT id,stat FROM hf_mes_user WHERE user='$user' AND pwd='".strtoupper(md5($password))."' LIMIT 1";
         $ret = sql_query($sql);
         if(count($ret) <= 0){
             return [
                'error_code' => 200,
                'error_msg' => "用户名或密码错误",
                'data' => [
                  
                ]
             ];
         }

        if($ret[0]['stat'] != 1){
            return [
                'error_code' => 200,
                'error_msg' => "当前不能登录",
                'data' => [

                ]
            ];
        }
         
         $user_id = $ret[0]['id'];
         $access_token = self::createToken($ret[0]['id']);
         $expires_in = TokenCache::$expires_in;
         $access_token_time = date("Y-m-d H:i:s");
         // 更新token信息
         $sql = "UPDATE hf_mes_user SET access_token='$access_token',expires_in='$expires_in',access_token_time='$access_token_time' WHERE id='$user_id'";
         $ret = sql_query($sql);
         if($ret === NULL){
             return [
                'error_code' => 200,
                'error_msg' => "更新access_token失败，请检查网络",
                'data' => [
                  
                ]
             ];
         }
           
         return [
            'error_code' => 0,
            'data' => [
              'user' => $user,
              'user_id' => $user_id,
              'access_token' => $access_token,
              "expires_in" => $expires_in,
              "access_token_time" => $access_token_time
            ]
         ];
    }

    /**
     * 检查 Token是否正确
     *
     * @param  $access_token
     * @return array
     */
    public static function checkAccessToken($access_token)
    {
        $sql = "SELECT id,expires_in,access_token_time FROM hf_mes_user WHERE access_token='$access_token' LIMIT 1";
        $ret = sql_query($sql);
        if(count($ret)<=0){
            return [
                'error_code' => 201,
                'error_msg' => "access_token不存在",
                'data' => [
                  
                ]
            ];
        }
        $user_id = $ret[0]['id'];
        $expires_in = $ret[0]['expires_in'];
        $access_token_time = strtotime($ret[0]['access_token_time']);
        if(time()-$access_token_time > $expires_in){
            return [
                'error_code' => 202,
                'error_msg' => "access_token已过期，请重新登录获取",
                'data' => [
                  
                ]
            ];
        }
        self::updateAccessTokenTime($user_id);
        return [
            'error_code' => 0,
            'error_msg' => "",
            'data' => [
              
            ]
        ];
        
        
    }

    /**
     * 获取 Token用户信息
     *
     * @param  $access_token
     * @return array
     */
    public static function getToken($access_token)
    {
        $sql = "SELECT * FROM hf_mes_user WHERE access_token='$access_token' LIMIT 1";
        $ret = sql_query($sql);
        return [
            'error_code' => 0,
            'data' => [
                'user_data'=>$ret
            ]
        ];
    }

    /**
     * 更新 AccessTokenTime
     *
     * @param  $access_token
     * @return array
     */
    private static function updateAccessTokenTime($user_id)
    {
        $access_token_time = date("Y-m-d H:i:s");
        $sql = "UPDATE hf_mes_user SET access_token_time='$access_token_time' WHERE id='$user_id'";
        sql_query($sql);
    }

    
}


class TokenCache{
    
    public static $expires_in  = 3600;
   
}

?>